import 'dart:async';
import 'dart:ffi';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  var onError = FlutterError.onError;
  FlutterError.onError = (FlutterErrorDetails details) {
    onError?.call(details);
    // ... 处理上报逻辑，此处是flutter自己捕获的异常
  };

  runZoned(() => runApp(const MyApp()),
      // 异常配置
      zoneSpecification: ZoneSpecification(
        // 捕获打印
        print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
          // ...收集日志
          // ...
          // 打印 此处可判断是否是debug模式下，如果是再打印
          parent.// print(zone, "Interceptor: $line");
        },
        // 捕获未处理的异步异常
        handleUncaughtError: (self, parent, zone, error, stackTrace) {
          parent.// print(zone, '${error.toString()} $stackTrace');
          // 处理异常
          // 。。。
        },
      ));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(
            seedColor: const Color.fromARGB(255, 66, 157, 218)),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'APP Name'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final ScrollController _controller = ScrollController();
  List<int> _list = [];
  @override
  void initState() {
    for (var i = 0; i < 100; i++) {
      _list.add(i);
    }

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // ignore: avoid_print
    // print("build");
    return Scaffold(
        appBar: AppBar(
            title: GestureDetector(
                // onTap: () => _controller.jumpTo(0.0),// 跳到固定位置
                onTap: () => _controller.animateTo(
                      -20,
                      curve: Curves.linear,
                      duration: const Duration(seconds: 3),
                    ), // 跳到固定位置 动画效果
                child: const Text("list demo"))),
        // 下拉刷新
        body: RefreshIndicator(
            onRefresh: () async {
              await Future.delayed(const Duration(seconds: 3));
              setState(() {
                _list = [
                  5,
                  6,
                  7,
                  6,
                  6,
                  5,
                  4,
                  6,
                  5,
                  4,
                  4,
                  6,
                  4,
                ];
              });
            },
            child:
                // NotificationListener // 拦截事件widget
                Scrollbar(
                    // isAlwaysShown: true,
                    controller: _controller,
                    child: ListView.builder(
                      controller: _controller,
                      itemExtent: 100, // 每一项在主轴上的高度
                      cacheExtent: 1000,
                      itemBuilder: (context, index) {
                        // ignore: avoid_print
                        // print("list$index");
                        return Container(
                          alignment: Alignment.center,
                          color: index % 2 != 0 ? Colors.blue : Colors.yellow,
                          child: Text('${_list[index]}'),
                        );
                      },
                      itemCount: _list.length,
                    ))));
  }
}
